package cn.xiangxiang.service.impl;

import cn.xiangxiang.mapper.goods.SpecMapper;
import cn.xiangxiang.mapper.goods.TemplateMapper;
import cn.xiangxiang.pojo.goods.Spec;
import cn.xiangxiang.pojo.goods.Template;
import cn.xiangxiang.service.goods.TemplateService;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = TemplateService.class)
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper,Template> implements TemplateService {

    @Autowired
    private SpecMapper specMapper;

    @Override
    public Page<Template> findConditionPage(Integer startpage, Integer pagesize, Template template) {
        Page<Template> page = new Page<>(startpage, pagesize);
        QueryWrapper<Template> wrapper = getCondition(template);
        Page<Template> templatePage = this.page(page, wrapper);
        return templatePage;
    }

    @Override
    public void delById(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("template_id",id);
        List<Spec> specList = specMapper.selectByMap(map);
        if(specList.size()>0){
            throw  new RuntimeException("该模板下存在规格，无法删除");
        }

        removeById(id);
    }

    private QueryWrapper<Template> getCondition(Template template) {
        QueryWrapper<Template> wrapper = new QueryWrapper<>();

        if(template!=null){
            if(template.getName()!=null && !"".equals(template.getName().trim())){
                wrapper.like("name",template.getName());
            }
        }
        return wrapper;
    }
}
